/**
 * Based on http://kb.mozillazine.org/Dev_:_Extensions_:_Example_Code_:_Adding_items_to_menus#Determining_the_visibility_of_context_menu_items
 * http://lxr.mozilla.org/aviarybranch/source/browser/base/content/browser.js
 */

var isInitialized=false;

//In the initOverlay() function add a popupshowing event listener for the menu you are extending, like this:
function initOverlay() {
    if (! isInitialized ){
        dump("\n initOverlay " + isInitialized);
        var menu = document.getElementById("contentAreaContextMenu");
        menu.addEventListener("popupshowing", contextPopupShowing, false);

        //Register SideBar SearchService
        Trucoder.SearchService.registerSearchObserver ();
        isInitialized=true;
    }
}

var isPanelShowing=false;
function activateSearchScreen(){
    if(isPanelShowing){
        try{
            //Enumerate all the windows and close the dialog  if open
            var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
            var windows = wm.getEnumerator ("trucoder:searchpanel");
            while(windows.hasMoreElements()){
                var win=windows.getNext();
                win.close();
            }
        }catch(e) {}//Suppressed

        isPanelShowing=false;
        return;
    }
    window.openDialog("chrome://footbar/content/searchpanel.xul", "", "chrome,dialog=yes,titlebar=no,popup=yes", null);
    isPanelShowing=true;

}


//Only show add to snippets if there is selection under the cursor
function contextPopupShowing() {
    var menuitem = document.getElementById("trucoder-cnt-save-selected");
    if(menuitem){
        menuitem.hidden = !gContextMenu.isTextSelected;
    }
}

//Shows Selected Text 
function showSelectedText(){
    //For this Plugin window.getSelection method will be called but for completion I have included other detection methods  as well
    var txt = '';
    var title='';
    var selectedBrowser = getBrowser().selectedBrowser;
    var webNav = selectedBrowser.webNavigation;
    var url = webNav.currentURI.spec;
    var doc = webNav.document;
    var title  = doc.title || url;

    ///txt=window.content.getSelection();

    var selObj = window.content.getSelection();
    var selRange = selObj.getRangeAt(0);

    txt=selRange;

    var gSidebarBox = document.getElementById('sidebar-box');
    var sidebar = document.getElementById('sidebar');
    ///var sidebarView = sidebar.contentDocument.defaultView;
    var gSidebarDoc = sidebar.contentDocument;
    //alert(title + '\n' + txt);
    //This is the side bar component forceFocus
    toggleSidebar('viewEmptySidebar', true);

    //Set focus to the
    //gSidebarDoc.getElementById('footbar-tab-panels').selectedIndex=1;
    Footbar.TabWatcher.setFocus('footbar-footer-tabs', 1, gSidebarDoc);

    var tru_snippet_code=gSidebarDoc.getElementById('tru_snippet_code');
    tru_snippet_code.value=txt;

    var tru_snippet_title=gSidebarDoc.getElementById('tru_snippet_title');
    tru_snippet_title.value=title;

}

function initSideBar(){
    dump("\n Initing SideBar ");
    /*
    var gSidebarBox = document.getElementById('sidebar-box');
     dump("\n 1"+gSidebarBox)
     var sidebar = document.getElementById('sidebar');
     dump("\n 2"+sidebar)
     var gSidebarDoc = sidebar.contentDocument;
     dump("\n 3"+gSidebarDoc)
     toggleSidebar('viewEmptySidebar', false);
     dump("\n 4"+gSidebarDoc)
     Footbar.TabWatcher.setFocus('footbar-footer-tabs', 1, gSidebarDoc);
     dump("\n 5")
     */

    toggleSidebar('viewEmptySidebar', false);

}
window.addEventListener("load", initOverlay, false);